﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YBApproval.IDAL;
using YBApproval.BLL;
using YBApproval.Model;
using System.Text;
using System.Collections.Specialized;

namespace YBApproval.UserControls
{
    public partial class UCApprover : System.Web.UI.UserControl
    {
        private IApproval _approvalDAL = DALFactory.GetDAL<IApproval>();
        private List<ApprovalView> _listApproval;

        public int ProcessId { get; set; }
        public int NodeOrder { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            GetApprovers();
        }

        private void GetApprovers()
        {
            _listApproval = _approvalDAL.GetApprovals(ProcessId, NodeOrder);
            foreach (ApprovalView ap in _listApproval)
            {
                TableRow row = new TableRow();
                TableCell cell = new TableCell();
                TextBox texbox = new TextBox();
                texbox.ID = "approver_text" + ap.Id;
                texbox.Text = ap.ApproverName;
                cell.Controls.Add(texbox);

                System.Web.UI.HtmlControls.HtmlInputButton button = new System.Web.UI.HtmlControls.HtmlInputButton();
                button.Value = "...";
                button.Attributes.Add("onclick", "selectApprover(this)");
                cell.Controls.Add(button);

                HiddenField hField = new HiddenField();
                hField.ID = "approver_" + ap.Id;
                hField.Value = ap.ApproverId.ToString();
                cell.Controls.Add(hField);

                HiddenField hFieldType = new HiddenField();
                hFieldType.ID = "approver_isrole" + ap.Id;
                hFieldType.Value = ap.IsRole.ToString();
                cell.Controls.Add(hFieldType);

                row.Cells.Add(cell);
                ApproverTable.Rows.Add(row);
            }
        }

        public List<ApprovalView> GetValues()
        {
            NameValueCollection collection = this.Request.Form;
            foreach (ApprovalView item in _listApproval)
            {
                string key = (from k in collection.AllKeys
                              where k.EndsWith("$approver_" + item.Id)
                              select k).SingleOrDefault();
                item.ApproverId = collection[key];
                key = (from k in collection.AllKeys
                       where k.EndsWith("$approver_text" + item.Id)
                       select k).SingleOrDefault();
                item.ApproverName = collection[key];
                key = (from k in collection.AllKeys
                       where k.EndsWith("$approver_isrole" + item.Id)
                       select k).SingleOrDefault();
                item.IsRole = bool.Parse(collection[key]);
            }
            return _listApproval;
        }

        public override void Dispose()
        {
            _approvalDAL.Dispose();
            base.Dispose();
        }
    }
}